#include "BinNode.h"
#include <iostream>
#include <queue>

using namespace std;

template <typename T>
BinNode<T>* BinNode<T>::insertAsLC(const T &e){
	return lc = new BinNode(e,this);
}

template <typename T>
BinNode<T>* BinNode<T>::insertAsRC(const T &e){
	return rc = new BinNode(e,this);
}

template <typename T>
BinNode<T>* BinNode<T>::succ(){
	BinNode<T>* s = this;
	if(rc){
		s = rc;
		while(HasLChild(*s)) s = s->lc;
	}
	else{
		while(IsRChild(*s)) s = s->parent;
		s = s->parent;
	}
	return s;
}

template <typename T>
void visit(const T &data){
	cout<<data<<endl;
}

template <typename T> 
int stature(BinNode<T> *x){
	int left = 0,right = 0;
	if(HasLChild(*x))
		left = stature(x->lc);
	if(HasRChild(*x))
		right = stature(x->rc);
	return left > right ? left+1 : right+1;
		
}

template <typename T> template <typename VST>
void BinNode<T>::travLevel(VST &visit){
	queue<BinNode<T> *> QNode;
	QNode.push(this);
	while(!QNode.empty()){
		BinNode<T> *QHead = QNode.pop();
		visit(QHead->data);
		if(HasLChild(*QHead))
			QNode.push(QHead->lc);
		if(HasRChild(*QHead))
			QNode.push(QHead->rc);
	}
}
